<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mysql
 *
 * @author Nhan Nguyen
 */
defined('IN_ADMIN') or die('Hacking Attempt!');

final class mysql extends DBConfig {
    private static $_sConnection;
    public static $_iLastId;
    
    public static function connectDB(){
        if(isset(DBConfig::$_sHOST) && isset(DBConfig::$_sUSER) &&
           isset(DBConfig::$_sPASS) && isset(DBConfig::$_sDB)){
            mysql::$_sConnection = new mysqli(DBConfig::$_sHOST, DBConfig::$_sUSER, DBConfig::$_sPASS, DBConfig::$_sDB);
            if(mysqli_connect_error()){
                die('Connection error ('. mysqli_connect_errno() .') '. mysqli_connect_error() .'.');
            }
        }else{
            die('Connection failed. Please check your config file.');
        }
    }

    public static function begin($iTransaction = 0){
        if($iTransaction)
            mysql::querySql('BEGIN');
        else
            return true;
    }

    public static function rollBack(){
        mysql::querySql('ROLLBACK');
    }

    public static function commit(){
        mysql::querySql('COMMIT');
    }

    public static function selectDebitAmount($sTable, $iCustomerId){
        $sSql = 'SELECT amount FROM '. $sTable .' WHERE customerId='. $iCustomerId;
        $aResult = mysql::querySql($sSql);
        $aRow = mysql::fetchSql($aResult);
        if($aRow)
            return $aRow;
        else
            return false;
    }

    public static function selectInventoryQuantity($sTable, $iInventoryId){
        $sSql = 'SELECT title, quantity FROM '. $sTable .' WHERE inventoryId='. $iInventoryId;
        $aResult = mysql::querySql($sSql);
        $aRow = mysql::fetchSql($aResult);
        if($aRow)
            return $aRow;
        else
            return false;
    }

    public static function insertSql($sTable, $aVals){
        $sSql = 'INSERT INTO '. $sTable .' VALUES (NULL';
        foreach($aVals as $key=>$value){
            $sSql .= ", '$value'";
        }
        $sSql .= ')';
        $iId = mysql::querySql($sSql);
        mysql::$_iLastId = mysql::$_sConnection->insert_id;
        
        return mysql::$_iLastId;
    }

    public static function updateSql($sTable, $aVals, $sField = null, $sCond = null){
        $sSql = 'UPDATE '. $sTable .' SET ';
        foreach($aVals as $key=>$value){
            $sSql .= $key ." = '$value', ";
        }
        $sSql[strlen($sSql) - 2] = '  ';
        $sSql .= "WHERE $sField=". $sCond;

        return mysql::querySql($sSql);
    }

    public static function deleteSql($sTable, $sField, $iId){
        $sSql = 'DELETE FROM '. $sTable .' WHERE '. $sField . '='. $iId;
        return mysql::querySql($sSql);
    }

    public static function getLastId(){
        return mysql::$_iLastId;
    }

    public static function querySql($sSql){        
        if($iId = mysql::$_sConnection->query($sSql))           
            return $iId;
        else
            die('Query failed ('. mysql_error () .').');
    }

    public static function fetchSql($aResult){
        return $aResult->fetch_assoc();
    }

    public static function closeDB(){
        if(mysql::$_sConnection != '')
            mysql::$_sConnection->close();
        else
            die('Cannot close connection.');
    }
}
?>
